package com.starinside.wp.service.cookbook.impl;


import com.starinside.wp.domain.cookbook.SimpleCookbook;
import com.starinside.wp.foundation.dto.StaffDTO;
import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.repository.cookbook.SimpleCoookbookRepository;
import com.starinside.wp.service.UploadService;
import com.starinside.wp.service.cookbook.SimpleCookbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

;

@Service
public class SimpleCookbookServiceImpl implements SimpleCookbookService {

    @Autowired
    SimpleCoookbookRepository baseRepository;

    @Autowired
    UploadService uploadService;

    @Override
    public SimpleCookbook get(String areaId, String id) {
        SimpleCookbook ret = baseRepository.getOneByIdAndAreaIdAndDeletedIsFalse(id, areaId);

        if (ret == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "对象不存在");
        }

        return ret;
    }

    @Override
    public Page<SimpleCookbook> page(String areaId, PagerQuery pagerQuery) {
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize(), Sort.Direction.DESC, "created");
        return baseRepository.findAllByAreaIdAndDeletedIsFalse(areaId, pageRequest);
    }
}
